/**
 * 部门管理office.js
 * 版本号： v1.0.0
 */
$(function () {
    /**
     *部门列表
     */
    var lastChecked = 0;
    $("#office_dataList").treegrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        border: false,
        pagination: true,
        striped: true,
        pageSize: 10,
        pageList: [10, 50, 100, 150, 200],
        url: CONTEXT_PATH + '/office/getOfficeTree.do',
        idField: 'officeId',
        treeField: "name",
        frozenColumns: [[
            {field: 'officeId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'name', title: '项目部名称', width: 200},
                {field: 'cname', title: '所属公司', width: 240, fixed: true},
                {field: 'principal', title: '项目部负责人', width: 120, fixed: true},
                {field: 'principalTel', title: '联系人电话', width: 120, fixed: true},
                {field: 'landlinePhone', title: '固定电话', width: 120, fixed: true},
                {field: 'createDateFormat', title: '创建时间', width: 135, fixed: true}
            ]
        ],
        onCheck: function (rowIndex, rowData) {
            var row1 = $("#office_dataList").treegrid("getChecked");
            if (row1 && rowIndex == lastChecked) {
                $('#office_dataList').treegrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            } else {
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (row, data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#office_dataList").treegrid("selectRow", data.rows[0].officeId);
            }
        }
    });

    /**
     * 获取部门combotree
     */
    $.ajax({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        type: "get",
        success: function (data) {
            var themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
            themecombo += data.substring(1);
            themecombo = JSON.parse(themecombo);
            $("#parentIdTree").combotree("loadData", themecombo);
            $("#parentIdTree").combotree({
                onSelect: function (node) {
                    $('#companyId').val(node.id);
                }
            });
        }
    });

    //定义可操作按钮
    $("#office_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

    //查询所属公司下拉树
    $.ajax({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        type: "get",
        success: function (data) {
            var themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
            themecombo += data.substring(1);
            themecombo = JSON.parse(themecombo);
            $("#companyTree").combotree("loadData", themecombo);
            $("#companyTree").combotree({
                onSelect: function (node) {
                    $('#companyId_').val(node.id);
                }
            });
        }
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            office_loadSearch();
                        }
                    });
                }
                //添加按钮
                if ("addBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            window.location.href = CONTEXT_PATH + "/office/toOffice.do";
                        }
                    });
                }
                //删除按钮
                if ("delBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            office_del();
                        }
                    });
                }
                //修改按钮
                if ("editBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            office_edit();
                        }
                    });
                }

                //查看部门
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                             office_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }

            }
        }
        return toolbar;
    }
}


/**
 * 查看部门
 */
function office_view(iconCls) {

    var checked = $("#office_dataList").treegrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择需要查看的项目部详情", timeout: 3000, showType: "slide"});
        return;
    }
    var row = $("#office_dataList").treegrid("getSelected");

    //IFRAM打开窗口
    layoutSub(bmxq.id ,bmxq.url + row.officeId, iconCls, bmxq.closable, bmxq.title);
}


/**
 * 删除部门
 */
function office_del() {
    var checked = $("#office_dataList").treegrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择要删除的部门", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    var row = $("#office_dataList").treegrid("getSelected");
    var node = $('#office_dataList').treegrid('getChildren', row.officeId);
    if (node.length > 0) {
        $.messager.show({title: '操作提示', msg: "父级部门不能删除，请重新选择", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    $.messager.confirm('删除提示', '您确定要删除 ' + row.name + ' 吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/office/del.do?officeId=" + row.officeId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#office_dataList").treegrid("reload", {
                        //查询数据
                        companyId: $("#companyId").val(),
                        name: $("#name").textbox('getValue')
                    });
                }
            })
        }
    });
}


/**
 * 修改部门
 * @returns {boolean}
 */
function office_edit() {
    var checked = $("#office_dataList").treegrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择修改的部门", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    var row = $("#office_dataList").treegrid("getSelected");
    window.location.href = CONTEXT_PATH + "/office/toOffice.do?officeId=" + row.officeId;
}


/**
 * 查询部门
 */
function office_loadSearch() {
    refreshTreePageNumber("office_dataList");
    $("#office_dataList").treegrid("reload", {
        //查询数据
        companyId: $("#companyId").val(),
        name: $("#name").textbox('getValue')
    });
    $("#office_dataList").treegrid("clearSelections");
    $('#officeQuery_dialog').dialog('close');
}


/***
 * 动态刷新页面
 * @param prId
 */
function refreshTreePageNumber(csId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + csId).treegrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + csId).treegrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}









